
var User = function(name,email,password){
      this.Name = name;
      this.Email = email;
      this.Password = password;
};
User.prototype = {
      constructor:User,
      set Name(emailValue){
            this.UserName =emailValue.slice(0,emailValue.indexOf("@"));
      },
      set Email(emailValue){
            this.UserEmail=emailValue;
      },
      set Password(passwordValue){
            this.UserPassword = passwordValue;
      },
      get Name(){
            return this.UserName;
      },
      get Email(){
            return this.UserEmail;
      },
      get Password(){
            return this.UserPassword;
      },
      toString:function(){
            return   this.Name + "/" + this.Email + "/" + this.Password ;
      }
};

var arr=[];
var key = "All_Users_List";


$('#buttonSubmit').on("click",function(){

      var emailValue = $('#emailField').val();
      var passwordValue = $('#passwordField').val();

      var emailField =  $('#emailField');
      var passwordField = $('#passwordField');

      var emailLabel = $('label[for="emailField"]');
      var passwordlabel = $('label[for="passwordField"]');

      var name,e_mail,password;

      var reg = new RegExp("(?=^.{8,}$)((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$");
      var reg2 = new RegExp("^[a-zA-Z0-9]+@([a-zA-Z0-9]+\.)+[A-z]{2,4}$");

      var passwordvalid = reg.test(passwordValue);
      var emailvalid = reg2.test(emailValue);

      if(emailvalid){
            e_mail=emailValue;
            name=emailValue;
            emailLabel.text("Логин введен").css({"color":"green"});
            emailField.css({"border":"3px solid green"});
      }else{
            emailLabel.text("Введите логин !!").css({"color":"red"});
            emailField.css({"border":"3px solid red"});
      }

      if(passwordvalid){
            password=passwordValue;
            passwordlabel.text("Пароль введен").css({"color":"green"});
            passwordField.css({"border":"3px solid green"});

      }else{
            passwordlabel.text("Введите пароль!!").css({"color":"red"});
            passwordField.css({"border":"3px solid red"});
      }

      if(emailvalid && passwordvalid){
            console.log('all is valid');
            var hav;
            for(var kk in arr){

                  var vc = JSON.parse(arr[kk]);
                  hav =  (vc.UserEmail==e_mail);

            }
            if(hav){
                  emailLabel.text("Есть такой э-адрес").css({"color":"red"});
                  emailField.css({"border":"3px solid red"});
                  console.log('have such user');
            }else{
                  console.log('dont have such user');
                  arr.push(JSON.stringify(new User(name,e_mail,password)));
                  localStorage.setItem(key,arr);
                  //window.open("#/users");
                  setTimeout(function(){
                        $('#popUp').css({display:"none"});
                        emailField.val("");
                        passwordField.val("");
                        emailLabel.text("Введите логин").css({"color":"white"});
                        emailField.css({"border":"3px solid  #3b505e"});
                        passwordlabel.text("Введите пароль").css({"color":"white"});
                        passwordField.css({"border":"3px solid  #3b505e"});
                  },5000);
            }
      }
      return false;
});


